using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Pavilion;

namespace Pavilion.Control
{
    public class MessageBox : Control
    {
        static private MessageBox instance;

        private Panel panel;
        private Label label;
        private Button okButton, yesButton, noButton, cancelButton;

        public event EventHandler CancelPressed, OkPressed, YesPressed, NoPressed;

        public MessageBox() : base()
        {
            instance = this;

            AreaBounds = new Rectangle(GameEngine.WindowWidth / 2 - 250, GameEngine.WindowHeight / 2 - 100, 500, 200);
            panel = new Panel(new Rectangle(0, 0, AreaBounds.Width, AreaBounds.Height));
            panel.BackgroundColor = new Color(0, 0, 0, 128);
            panel.OutlineColor = Color.White;
            label = new Label("", new Rectangle(5, 60, panel.AreaBounds.Width - 10, 20), Color.Transparent);
            label.OutlineColor = Color.Transparent;
            label.TextSize = 0.3f;

            okButton = new Button("Ok", new Rectangle(panel.AreaBounds.Width / 2 - 50, 130, 100, 40), 0.4f);
            yesButton = new Button("Yes", new Rectangle(panel.AreaBounds.Width - 340 - ((panel.AreaBounds.Width - 340) / 2), 130, 100, 40), 0.4f);
            noButton = new Button("No", new Rectangle(panel.AreaBounds.Width - 220 - ((panel.AreaBounds.Width - 340) / 2), 130, 100, 40), 0.4f);
            cancelButton = new Button("Cancel", new Rectangle(panel.AreaBounds.Width - 100 - ((panel.AreaBounds.Width - 340) / 2), 130, 100, 40), 0.4f);

            okButton.Visible = false;
            yesButton.Visible = false;
            noButton.Visible = false;
            cancelButton.Visible = false;

            this._Collection.Add(panel);
            panel.Collection.Add(label);
            panel.Collection.Add(okButton);
            panel.Collection.Add(yesButton);
            panel.Collection.Add(noButton);
            panel.Collection.Add(cancelButton);

            okButton.Pressed += new Action(OnOkButton_Pressed);
            yesButton.Pressed += new Action(OnYesButton_Pressed);
            noButton.Pressed += new Action(OnNoButton_Pressed);
            cancelButton.Pressed += new Action(OnCancelButton_Pressed);
        }

        static public void StaticShowMessage(string text)
        {
            new MessageBox().ShowMessage(text);
        }

        public void ShowMessage(string text)
        {
            okButton.Visible = true;

            Display(text);
        }

        public void ShowYesNoMessage(string text)
        {
            yesButton.Visible = true;
            noButton.Visible = true;
            cancelButton.Visible = true;

            Display(text);
        }

        private void Display(string text)
        {
            label.Text = text;
            RootControl.Instance.Collection.Add(this);
        }

        private void Hide()
        {
            if (Parent != null)
                DetachFromParent();

            okButton.Visible = false;
            yesButton.Visible = false;
            noButton.Visible = false;
            cancelButton.Visible = false;
        }

        private void OnCancelButton_Pressed()
        {
            Hide();

            if (CancelPressed != null)
                CancelPressed(this, EventArgs.Empty);
        }

        private void OnNoButton_Pressed()
        {
            Hide();

            if (NoPressed != null)
                NoPressed(this, EventArgs.Empty);
        }

        private void OnYesButton_Pressed()
        {
            Hide();

            if (YesPressed != null)
                YesPressed(this, EventArgs.Empty);
        }

        private void OnOkButton_Pressed()
        {
            Hide();

            if (OkPressed != null)
                OkPressed(this, EventArgs.Empty);
        }
    }
}
